package com.devplatform.service.impl.points;

import com.devplatform.mapper.points.PointsDetailMapper;
import com.devplatform.mapper.points.UserPointsMapper;
import com.devplatform.pojo.points.PointsDetail;
import com.devplatform.pojo.points.UserPoints;
import com.devplatform.service.points.PointsCommonService;
import com.devplatform.util.MessageUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Service
public class PointsCommonServiceImpl implements PointsCommonService {
    @Resource
    private UserPointsMapper userPointsMapper;
    @Resource
    private PointsDetailMapper pointsDetailMapper;
    @Resource
    private MessageUtil messageUtil;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updatePointsDetail(Long userId, String reasonCode, Integer pointsChange, boolean sendNotice) {
        // 查询用户当前积分，若不存在则初始化
        UserPoints userPoints = userPointsMapper.selectByUserId(userId);
        if (userPoints == null) {
            userPoints = new UserPoints();
            userPoints.setUserId(userId);
            userPoints.setTotalPoints(0);
            userPoints.setPoints(0);
            userPoints.setChangeTime(LocalDateTime.now());
            userPointsMapper.insert(userPoints);
        }

        // 检查积分是否足够（仅扣积分时需要）
        if (pointsChange < 0 && userPoints.getPoints() + pointsChange < 0) {
            throw new RuntimeException("可用积分不足，无法执行");
        }

        // 更新可用积分和时间
        userPoints.setPoints(userPoints.getPoints() + pointsChange);
        userPoints.setChangeTime(LocalDateTime.now());

        // 增加积分时同步更新总积分
        if (pointsChange > 0) {
            userPoints.setTotalPoints(userPoints.getTotalPoints() + pointsChange);
        }

        userPointsMapper.updateById(userPoints);

        // 记录积分明细
        PointsDetail pointsDetail = new PointsDetail();
        pointsDetail.setUserId(userId);
        pointsDetail.setReasonCode(reasonCode);
        pointsDetail.setPointsChange(pointsChange);
        pointsDetail.setCreateTime(LocalDateTime.now());
        pointsDetailMapper.insert(pointsDetail);
        if (sendNotice) {
            String action = getActionName(reasonCode);
            messageUtil.sendPointsMessage(userId, action, pointsChange, userPoints.getPoints());
        }
    }

    private String getActionName(String reasonCode) {
        switch (reasonCode) {
            case "ARTICLE_LIKE":
                return "文章点赞";
            case "ARTICLE_REPLY":
                return "回复评论";
            case "ARTICLE_PUBLISH":
                return "发布文章";
            case "SIGN_DAILY":
                return "每日签到";
            case "SIGN_CONTINUOUS_7":
                return "连续签到7天";
            case "SIGN_CONTINUOUS_30":
                return "连续签到30天";
            case "EXCHANGE_GOODS":
                return "兑换商品";
            case "INCREASE_HOT":
                return "兑换热度";
            case "ARTICLE_POINTS":
                return "文章分享";
            default:
                return reasonCode;
        }
    }

    @Override
    public int getCurrentPoints(Long userId) {
        UserPoints userPoints = userPointsMapper.selectByUserId(userId);
        return userPoints != null ? userPoints.getPoints() : 0;
    }
}
